package com.loukou.lkt.cron.task;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loukou.lkt.lkt.dao.StockInOrderDao;
import com.loukou.lkt.lkt.dao.StockInOrderDetailDao;
import com.loukou.lkt.lkt.dao.StockOutOrderDao;
import com.loukou.lkt.lkt.dao.StockOutOrderDetailDao;
import com.loukou.lkt.lkt.entity.StockInOrder;
import com.loukou.lkt.lkt.entity.StockInOrderDetail;
import com.loukou.lkt.lkt.entity.StockOutOrder;
import com.loukou.lkt.lkt.entity.StockOutOrderDetail;
import com.loukou.lkt.lkt.enums.ChangeStockTypeEnum;
import com.loukou.lkt.lkt.enums.IsModifyWebStockEnum;
import com.loukou.lkt.lkt.enums.StockInOrderTypeEnum;
import com.loukou.lkt.lkt.enums.StockOutOrderTypeEnum;
import com.loukou.lkt.lkt.req.ChangeStockReq;
import com.loukou.lkt.lkt.resp.RespDto;
import com.loukou.lkt.lkt.service.GoodsStockService;

/**
 * 推送库存
 * @author 荣
 *
 */
@Service("SendStockToPhpTask")
public class SendStockToPhpTask{
	Logger logger = Logger.getLogger(SendPurchaseStockInOrderTask.class);
	
	@Autowired
	private StockInOrderDao stockInOrderDao;
	@Autowired
	private StockInOrderDetailDao stockInOrderDetailDao;
	@Autowired
	private StockOutOrderDao stockOutOrderDao;
	@Autowired
	private StockOutOrderDetailDao stockOutOrderDetailDao;
	@Autowired
	private GoodsStockService goodsStockService;
	
	//推送采购入库单库存修改
	public void noticePurchaseStock() {
		//获取未推送的采购入库单
		List<StockInOrder> stockInList=stockInOrderDao.findByOrderTypeAndIsModifyWebStock(StockInOrderTypeEnum.TYPE_PURCHASE.getType(),IsModifyWebStockEnum.NO.getType());
		for(StockInOrder si:stockInList){
			List<ChangeStockReq> changeStockList=new ArrayList<ChangeStockReq>();
			List<StockInOrderDetail> stockInDetailList=stockInOrderDetailDao.findByOrderId(si.getId());
			for(StockInOrderDetail siod:stockInDetailList){
				ChangeStockReq changeStock=new ChangeStockReq();
				changeStock.setSpecId(siod.getGoodsId());
				changeStock.setQuantity(siod.getReceiveNumber());
				changeStockList.add(changeStock);
			}
			RespDto<Integer> result=goodsStockService.changeWebStock(changeStockList,ChangeStockTypeEnum.TYPE_PURCHASE_IN.getType());
			if (result.getCode()!=200) {
				logger.error("send purchase in order stock error!");
				logger.error("推送采购入库单id【" + si.getId() + "】失败，错误:"
						+ result.getMsg());
				continue;
			}
		}
	}
	
	//推送调拨入库单
	public void noticeAllocationInStock() {
		//获取未推送的调拨入库单
		List<StockInOrder> stockInList=stockInOrderDao.findByOrderTypeAndIsModifyWebStock(StockInOrderTypeEnum.TYPE_AllOCATION.getType(),IsModifyWebStockEnum.NO.getType());
		for(StockInOrder si:stockInList){
			List<ChangeStockReq> changeStockList=new ArrayList<ChangeStockReq>();
			List<StockInOrderDetail> stockInDetailList=stockInOrderDetailDao.findByOrderId(si.getId());
			for(StockInOrderDetail siod:stockInDetailList){
				ChangeStockReq changeStock=new ChangeStockReq();
				changeStock.setSpecId(siod.getGoodsId());
				changeStock.setQuantity(siod.getReceiveNumber());
				changeStockList.add(changeStock);
			}
			RespDto<Integer> result=goodsStockService.changeWebStock(changeStockList,ChangeStockTypeEnum.TYPE_ALLOCATION_IN.getType());
			if (result.getCode()!=200) {
				logger.error("send allocation in order stock error!");
				logger.error("推送调拨入库单id【" + si.getId() + "】失败，错误:"
						+ result.getMsg());
				continue;
			}
		}
	}
	
	//推送采购退货出库单库存修改
	public void noticePurchaseReturnStock() {
		//获取未推送的采购退货出库单
		List<StockOutOrder> stockOutList=stockOutOrderDao.findByOrderTypeAndIsModifyWebStock(StockOutOrderTypeEnum.TYPE_RETURN_GOODS.getType(),IsModifyWebStockEnum.NO.getType());
		for(StockOutOrder so:stockOutList){
			List<ChangeStockReq> changeStockList=new ArrayList<ChangeStockReq>();
			List<StockOutOrderDetail> stockOutDetailList=stockOutOrderDetailDao.findByOrderId(so.getId());
			for(StockOutOrderDetail sood:stockOutDetailList){
				ChangeStockReq changeStock=new ChangeStockReq();
				changeStock.setSpecId(sood.getGoodsId());
				changeStock.setQuantity(sood.getDeliveryNumber());
				changeStockList.add(changeStock);
			}
			RespDto<Integer> result=goodsStockService.changeWebStock(changeStockList,ChangeStockTypeEnum.TYPE_RETURN_OUT.getType());
			if (result.getCode()!=200) {
				logger.error("send purchase in order stock error!");
				logger.error("推送采购退货出库单id【" + so.getId() + "】失败，错误:"
						+ result.getMsg());
				continue;
			}
		}
	}
	
	//推送调拨出库单
	public void noticeAllocationOutStock() {
		//获取未推送的调拨出库单
		List<StockOutOrder> stockOutList=stockOutOrderDao.findByOrderTypeAndIsModifyWebStock(StockOutOrderTypeEnum.TYPE_AllOCATION.getType(),IsModifyWebStockEnum.NO.getType());
		for(StockOutOrder so:stockOutList){
			List<ChangeStockReq> changeStockList=new ArrayList<ChangeStockReq>();
			List<StockOutOrderDetail> stockOutDetailList=stockOutOrderDetailDao.findByOrderId(so.getId());
			for(StockOutOrderDetail sood:stockOutDetailList){
				ChangeStockReq changeStock=new ChangeStockReq();
				changeStock.setSpecId(sood.getGoodsId());
				changeStock.setQuantity(sood.getDeliveryNumber());
				changeStockList.add(changeStock);
			}
			RespDto<Integer> result=goodsStockService.changeWebStock(changeStockList,ChangeStockTypeEnum.TYPE_ALLOCATION_OUT.getType());
			if (result.getCode()!=200) {
				logger.error("send allocation in order stock error!");
				logger.error("推送调拨出库单id【" + so.getId() + "】失败，错误:"
						+ result.getMsg());
				continue;
			}
		}
	}
}
